package cn.xdd.servlet.poweManage;


import cn.xdd.dao.UserDao;
import cn.xdd.entity.User;
import cn.xdd.entity.UserAllTokens;
import cn.xdd.entity.power;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import static cn.xdd.dbutil.DbUtil.Count;

/**
 * @author user_xdd
 * @Date 2022-04-01 15:32
 * @description
 * @return
 */
@WebServlet(urlPatterns = {"/TokenGetServlet"})
public class TokenGetServlet extends GenericServlet {
    private static final Logger LOGGER = Logger.getLogger(TokenGetServlet.class);


    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("utf-8");//设置请求中文编码
        response.setContentType("text/html;charset=utf-8");//设置响应中文编码


        UserDao userDao = new UserDao();
        String option = request.getParameter("option");
         LOGGER.debug("    option:"+option);
        HttpSession session = request.getSession();
        Gson gson = new Gson();


        try {
            if (option != null && option.equals("getpowers")) {
//                初始化
                UserAllTokens userAllTokens = new UserAllTokens();
                UserAllTokens find = (UserAllTokens) request.getSession().getAttribute("userAllTokens");

//                检查是否第一次查询
                if (find == null) {
//                查询user
                    User loginUser = (User) session.getAttribute("loginUser");

                    LOGGER.debug("    loginUser:" + loginUser);
                    //------------------------------- -------------------------------
                if(loginUser==null){
                    response.getWriter().println("<script>alert('请登录')</script>");
                    response.sendRedirect("/login.html");
                    return;
                }
                //-------------------------------无效代码 -------------------------------
//                查找权限

                    userAllTokens.setUser(loginUser);
                    List<power> tokens = userDao.getTokenByID(loginUser.getUser_id());
                    userAllTokens.setPowers(tokens);
//                    存入session方便以后使用
                    request.getSession().setAttribute("userAllTokens", userAllTokens);
                    LOGGER.debug("    userAllTokens:" + userAllTokens);
                }else {
                    userAllTokens=find;
                     LOGGER.debug("    session中有:userAllTokens");
                }

                String json = gson.toJson(userAllTokens);


                response.getWriter().println(json);
            }
//        默认跳转到index.html
            else {
                LOGGER.error("当前用户没有操作，或者操作码错误 默认跳到index.html ");
                response.sendRedirect("/index.html");

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }   catch (NullPointerException e){
            response.sendRedirect("/login.html");

            e.printStackTrace();
            return;
        }


    }
}
